package com.zx.testCode;

import java.io.*;
import java.util.*;

public class Test15 {

	public String encrypt(String key, String data) {
		List<Character> charlist = new ArrayList<Character>();
		String miyao = key.toUpperCase();
		int length = miyao.length();
		for (int i = 0; i < length; i++) {
			char ch = miyao.charAt(i);
			if (charlist.isEmpty() || !(charlist.contains(ch))) {
				charlist.add(ch);
			}
		}
		for (int k = 'A'; k <= 'Z'; k++) {
			if (!(charlist.contains((char) k))) {
				charlist.add((char) k);
			}
		}
		StringBuffer sb = new StringBuffer();
		Iterator in = charlist.iterator();
		while (in.hasNext()) {
			sb.append(in.next());
		}
		Map<Character, Character> resmap = this.getMap(sb.toString());
		StringBuffer res = new StringBuffer();
		String inputstr = data.toUpperCase();
		for (int m = 0; m < inputstr.length(); m++) {
			char data_char = data.charAt(m);
			char input_char = inputstr.charAt(m);
			if (data_char >= 'a' && data_char <= 'z') {
				char res_char = resmap.get(input_char);
				res.append((char) (res_char + ('a' - 'A')));
			} else if (data_char >= 'A' && data_char <= 'Z') {
				char res_char = resmap.get(input_char);
				res.append(res_char);
			} else {
				res.append(data_char);
			}
		}
		return res.toString();
	}

	public Map<Character, Character> getMap(String str) {
		Map<Character, Character> map = new HashMap<Character, Character>();
		int length = str.length();
		char start_char = 'A';
		int count = 0;
		for (int i = 0; i < length; i++) {
			char ch = str.charAt(i);
			map.put((char) (start_char + count), ch);
			count++;
		}
		return map;
	}

	public static void main(String args[]) throws Exception {

		/**
		 * 符合IP地址的范围
		 */
		String oneAddress = "10.127.30.45";
		/**
		 * 符合IP地址的长度范围但是不符合格式
		 */
		String twoAddress = "127.30.45";
		/**
		 * 不符合IP地址的长度范围
		 */
		String threeAddress = "7.0.4";
		/**
		 * 不符合IP地址的长度范围但是不符合IP取值范围
		 */
		String fourAddress = "255.255.255.2529";

		// 判断oneAddress是否是IP
		System.out.println(Test.isIP(oneAddress));

		// 判断twoAddress是否是IP
		System.out.println(Test.isIP("255.255.255.0"));

		// 判断threeAddress是否是IP
		System.out.println(Test.isIP("192.168.0.1"));

		// 判断fourAddress是否是IP
		System.out.println(Test.isIP("192.168.0.254"));

	}

}
